<template>
  <div class="home">
    <home-header></home-header>  
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-recommend :list="recommendList"></home-recommend>
    <weekend :list="weekendList"></weekend>
  </div>
</template>

<script>
// @ is an alias to /src
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import Weekend from './components/Weekend'
import axios from 'axios'
import {mapState} from 'vuex'
export default {
  name: 'Home',
  components:{
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    Weekend,
  },
  // 将state中的city属性映射到computed中的city属性上
  computed:{
    ...mapState(['city'])
  },
  methods:{
    getHomeInfo(){
      axios.get('https://www.fastmock.site/mock/4c7a35990c15d8e6700c84cdc193ecc9/api/index?city='+this.city).then(res=>this.getHomeInfoSucc(res))
    },
    getHomeInfoSucc(res){
      console.log(res)
      res=res.data
      if(res.ret&&res.data){
        const data=res.data
        this.swiperList=data.swiperList
        this.iconList=data.iconList
        this.recommendList=data.recommendList
        this.weekendList=data.weekendList
      }
    }
  },
  // 页面重新显示时会执行的生命周期函数，即使页面不重新加载也会执行，只有组件在<keep-alive></keep-alive>中才会生效；
  // 如果lastCity不等于当前的city则执行getHomeInfo()函数，重新发送请求，并将lastCity赋值为当前的city
  activated(){
    if(this.lastCity!==this.city){
      this.getHomeInfo()
      this.lastCity=this.city
    }
  },
  data(){
    return {
      swiperList:[],
      iconList:[],
      recommendList:[],
      weekendList:[],
      lastCity:'',
    }
  }
}
</script>
